****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings

****************************************************************************

clear all 
cap log close
set more off
set matsize 800 	

use temp_main.dta, clear
by f2000, sort: egen min_test = min(lag_avg_test)

**CREATING NEIGH MEASURE OF PRICE RESIDUALS

**cbg 2000
global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt"
xi:reg LogPrice  $covars1 , r
predict resid, resid
by Highschoolcode salesyear, sort: egen HS_price_resid = mean(resid)
by Midschoolcode salesyear, sort: egen Mid_price_resid = mean(resid)
by Elemschoolcode salesyear, sort: egen Elem_price_resid = mean(resid)
drop resid

global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt renovationlast10yrs on_bigger_rd Infloodplain05 dist_park CBGPopDensity CBGMedianHHIncome PercUnemploy"

xi:reg LogPrice  $covars1  , r
predict resid, resid
by Highschoolcode salesyear, sort: egen HS_price_residV2 = mean(resid)
by Midschoolcode salesyear, sort: egen Mid_price_residV2 = mean(resid)
by Elemschoolcode salesyear, sort: egen Elem_price_residV2 = mean(resid)
drop resid

xi:reg LogPrice  $covars1 i.f2000 , r
predict resid, resid
by Highschoolcode salesyear, sort: egen HS_price_residCBGFEV2 = mean(resid)
by Midschoolcode salesyear, sort: egen Mid_price_residCBGFEV2 = mean(resid)
by Elemschoolcode salesyear, sort: egen Elem_price_residCBGFEV2 = mean(resid)
drop resid

xi:reg LogPrice  $covars1 , r
predict resid, resid
by Highschoolcode f2000 salesyear, sort: egen HSbyCBG_price_residV2 = mean(resid)
by Midschoolcode f2000 salesyear, sort: egen MidbyCBG_price_residV2 = mean(resid)
by Elemschoolcode f2000 salesyear, sort: egen ElembyCBG_price_residV2 = mean(resid)

by Highschoolcode salesyear f2000 , sort: egen HSbyCBG_salesprice_mean = mean(salesprice)
by Midschoolcode salesyear f2000 , sort: egen MidbyCBG_salesprice_mean= mean(salesprice)
by Elemschoolcode salesyear  , sort: egen Elem_salesprice_mean = mean(salesprice)

by Highschoolcode salesyear f2000 , sort: egen HSbyCBG_salesprice_median = median(salesprice)
by Midschoolcode salesyear f2000 , sort: egen MidbyCBG_salesprice_median = median(salesprice)
by Elemschoolcode salesyear  , sort: egen Elem_salesprice_median = median(salesprice)


keep Highschoolname Midschoolname Elemschoolname Highschoolcode Midschoolcode Elemschoolcode f2000 ///
HS_price_resid-Elem_salesprice_median salesyear
duplicates drop
save house_price_resid3.dta,replace


**CBG 2010
use temp_main.dta, clear

global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt"
xi:reg LogPrice  $covars1 , r
predict resid, resid
by Highschoolcode salesyear, sort: egen HS_price_resid = mean(resid)
by Midschoolcode salesyear, sort: egen Mid_price_resid = mean(resid)
by Elemschoolcode salesyear, sort: egen Elem_price_resid = mean(resid)
drop resid

global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt renovationlast10yrs on_bigger_rd Infloodplain05 dist_park  CBGPopDensity CBGMedianHHIncome PercUnemploy"

xi:reg LogPrice  $covars1  , r
predict resid, resid
by Highschoolcode salesyear, sort: egen HS_price_residV2 = mean(resid)
by Midschoolcode salesyear, sort: egen Mid_price_residV2 = mean(resid)
by Elemschoolcode salesyear, sort: egen Elem_price_residV2 = mean(resid)
drop resid

xi:reg LogPrice  $covars1 i.f2010 , r
predict resid, resid
by Highschoolcode  salesyear, sort: egen HS_price_residCBGFEV2 = mean(resid)
by Midschoolcode  salesyear, sort: egen Mid_price_residCBGFEV2 = mean(resid)
by Elemschoolcode  salesyear, sort: egen Elem_price_residCBGFEV2 = mean(resid)
drop resid

xi:reg LogPrice  $covars1 , r
predict resid, resid
by Highschoolcode f2010  salesyear, sort: egen HSbyCBG_price_residV2 = mean(resid)
by Midschoolcode f2010  salesyear, sort: egen MidbyCBG_price_residV2 = mean(resid)
by Elemschoolcode f2010  salesyear , sort: egen ElembyCBG_price_residV2 = mean(resid)


by Highschoolcode salesyear f2010 , sort: egen HSbyCBG_salesprice_mean = mean(salesprice)
by Midschoolcode salesyear f2010 , sort: egen MidbyCBG_salesprice_mean= mean(salesprice)
by Elemschoolcode salesyear  , sort: egen Elem_salesprice_mean = mean(salesprice)

by Highschoolcode salesyear f2010 , sort: egen HSbyCBG_salesprice_median = median(salesprice)
by Midschoolcode salesyear f2010 , sort: egen MidbyCBG_salesprice_median = median(salesprice)
by Elemschoolcode salesyear  , sort: egen Elem_salesprice_median = median(salesprice)


keep Highschoolname Midschoolname Elemschoolname Highschoolcode Midschoolcode Elemschoolcode f2010 ///
HS_price_resid-Elem_salesprice_median
duplicates drop
drop _Iyear_qt_2- resid
duplicates drop
save house_price_resid3_2010.dta,replace	
	
	
use temp_main.dta, clear

global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt"
xi:reg LogPrice  $covars1 , r
predict resid, resid
by Highschoolcode, sort: egen HS_price_resid = mean(resid)
by Midschoolcode, sort: egen Mid_price_resid = mean(resid)
by Elemschoolcode, sort: egen Elem_price_resid = mean(resid)
drop resid


global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt"
xi:reg LogPrice  $covars1  i.f2000 , r
predict resid, resid
by Highschoolcode, sort: egen HS_price_residCBGFE = mean(resid)
by Midschoolcode, sort: egen Mid_price_residCBGFE = mean(resid)
by Elemschoolcode, sort: egen Elem_price_residCBGFE = mean(resid)
drop resid

global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt"
xi:reg LogPrice  $covars1 , r
predict resid, resid
by Highschoolcode f2000, sort: egen HSbyCBG_price_resid = mean(resid)
by Midschoolcode f2000, sort: egen MidbyCBG_price_resid = mean(resid)
by Elemschoolcode f2000, sort: egen ElembyCBG_price_resid = mean(resid)
drop resid

global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt renovationlast10yrs on_bigger_rd Infloodplain05 dist_park  CBGPopDensity CBGMedianHHIncome PercUnemploy"

xi:reg LogPrice  $covars1 , r
predict resid, resid
by Highschoolcode, sort: egen HS_price_residV2 = mean(resid)
by Midschoolcode, sort: egen Mid_price_residV2 = mean(resid)
by Elemschoolcode, sort: egen Elem_price_residV2 = mean(resid)
drop resid

xi:reg LogPrice  $covars1 i.f2000 , r
predict resid, resid
by Highschoolcode, sort: egen HS_price_residCBGFEV2 = mean(resid)
by Midschoolcode, sort: egen Mid_price_residCBGFEV2 = mean(resid)
by Elemschoolcode, sort: egen Elem_price_residCBGFEV2 = mean(resid)
drop resid

xi:reg LogPrice  $covars1 , r
predict resid, resid
by Highschoolcode f2000, sort: egen HSbyCBG_price_residV2 = mean(resid)
by Midschoolcode f2000, sort: egen MidbyCBG_price_residV2 = mean(resid)
by Elemschoolcode f2000, sort: egen ElembyCBG_price_residV2 = mean(resid)

by Highschoolcode f2000 , sort: egen HSbyCBG_salesprice_mean = mean(salesprice)
by Midschoolcode  f2000 , sort: egen MidbyCBG_salesprice_mean= mean(salesprice)
by Elemschoolcode  , sort: egen Elem_salesprice_mean = mean(salesprice)

by Highschoolcode f2000 , sort: egen HSbyCBG_salesprice_median = median(salesprice)
by Midschoolcode  f2000 , sort: egen MidbyCBG_salesprice_median = median(salesprice)
by Elemschoolcode  , sort: egen Elem_salesprice_median = median(salesprice)



keep Highschoolname Midschoolname Elemschoolname Highschoolcode Midschoolcode Elemschoolcode f2000 ///
HS_price_resid- Elem_salesprice_median
duplicates drop
save house_price_resid2_rev.dta,replace


*2010 CBGs
use temp_main.dta, clear

global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt"
xi:reg LogPrice  $covars1 , r
predict resid, resid
by Highschoolcode, sort: egen HS_price_resid = mean(resid)
by Midschoolcode, sort: egen Mid_price_resid = mean(resid)
by Elemschoolcode, sort: egen Elem_price_resid = mean(resid)
drop resid

global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt "
xi:reg LogPrice  $covars1 i.f2010 , r
predict resid, resid
by Highschoolcode, sort: egen HS_price_residCBGFE = mean(resid)
by Midschoolcode, sort: egen Mid_price_residCBGFE = mean(resid)
by Elemschoolcode, sort: egen Elem_price_residCBGFE = mean(resid)
drop resid

global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt"
xi:reg LogPrice  $covars1 , r
predict resid, resid
by Highschoolcode f2010, sort: egen HSbyCBG_price_resid = mean(resid)
by Midschoolcode f2010, sort: egen MidbyCBG_price_resid = mean(resid)
by Elemschoolcode f2010, sort: egen ElembyCBG_price_resid = mean(resid)
drop resid

global covars1 "acres bathrooms sqft sqftsquared age agesquared agecubed fireplace DistCBD DistCBDsq DistInterstate i.year_qt renovationlast10yrs on_bigger_rd Infloodplain05 dist_park  CBGPopDensity CBGMedianHHIncome PercUnemploy"

xi:reg LogPrice  $covars1 , r
predict resid, resid
by Highschoolcode, sort: egen HS_price_residV2 = mean(resid)
by Midschoolcode, sort: egen Mid_price_residV2 = mean(resid)
by Elemschoolcode, sort: egen Elem_price_residV2 = mean(resid)
drop resid

xi:reg LogPrice  $covars1 i.f2010 , r
predict resid, resid
by Highschoolcode, sort: egen HS_price_residCBGFEV2 = mean(resid)
by Midschoolcode, sort: egen Mid_price_residCBGFEV2 = mean(resid)
by Elemschoolcode, sort: egen Elem_price_residCBGFEV2 = mean(resid)
drop resid

xi:reg LogPrice  $covars1 , r
predict resid, resid
by Highschoolcode f2010, sort: egen HSbyCBG_price_residV2 = mean(resid)
by Midschoolcode f2010, sort: egen MidbyCBG_price_residV2 = mean(resid)
by Elemschoolcode f2010, sort: egen ElembyCBG_price_residV2 = mean(resid)

by Highschoolcode f2010 , sort: egen HSbyCBG_salesprice_mean = mean(salesprice)
by Midschoolcode  f2010 , sort: egen MidbyCBG_salesprice_mean= mean(salesprice)
by Elemschoolcode  , sort: egen Elem_salesprice_mean = mean(salesprice)

by Highschoolcode f2010 , sort: egen HSbyCBG_salesprice_median = median(salesprice)
by Midschoolcode  f2010 , sort: egen MidbyCBG_salesprice_median = median(salesprice)
by Elemschoolcode  , sort: egen Elem_salesprice_median = median(salesprice)


keep Highschoolname Midschoolname Elemschoolname Highschoolcode Midschoolcode Elemschoolcode f2010 ///
HS_price_resid- Elem_salesprice_median
duplicates drop
drop _Iyear_qt_2- resid
duplicates drop
save house_price_resid2_2010_rev.dta,replace


	
	
	
	
	
	
	
	
	
	
	
